﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeakView.common;
using LeakView.service;
using System.Windows.Forms.DataVisualization.Charting;

namespace LeakView.misc.view
{
    public partial class YLPanel : UserControl
    {
        private MiscValueDataSource DsHead = new MiscValueDataSource(MiscType.HeadYL);
        private MiscValueDataSource DsTail = new MiscValueDataSource(MiscType.TailYL);
        
        public YLPanel()
        {
            InitializeComponent();
        }

        private void YLPanel_Load(object sender, EventArgs e)
        {
            UpdateTimeRange();

            foreach (Series series in this.chart1.Series)
            {
                series.ChartType = SeriesChartType.Spline;
                series.BorderColor = Color.Transparent;
                series.BorderWidth = 1;
                series.ChartArea = "ChartArea1";
                series.ChartType = SeriesChartType.Line;
                //series.Color = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
                series.IsXValueIndexed = false;
                //series.MarkerColor = Color.Blue;
                series.MarkerSize = 1;
                series.MarkerStyle = MarkerStyle.Square;
                series.ShadowColor = Color.Transparent;
                series.ShadowOffset = 1;
                series.XValueType = ChartValueType.Time;
            }

            this.chart1.Series[0].Color = Miscs.HeadColor;
            this.chart1.Series[1].Color = Miscs.TailColor;

            this.chart1.Series[0].LegendText = AppConfig.HeadName + ":" + double.NaN.ToString("F3");
            this.chart1.Series[1].LegendText = AppConfig.TailName + ":" + double.NaN.ToString("F3");

            var time = DateTime.Now.AddHours(-1);
            DsHead.Add(time, 70D);
            DsTail.Add(time, 70D);
            for (int i = 0; i < 100; i++)
            {
                time = time + TimeSpan.FromSeconds(10);
                DsHead.Add(time, double.NaN);
                DsTail.Add(time, double.NaN);
            }
            DsHead.Bind(chart1);
            DsTail.Bind(chart1);

        }

        public void UpdateTimeRange()
        {
            DsHead.MaxTimeSpan = Miscs.MiscChartTimeSpan;
            DsTail.MaxTimeSpan = Miscs.MiscChartTimeSpan;

            this.chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
            this.chart1.ChartAreas[0].AxisX.Interval = Miscs.MiscChartTimeMinutes / 6;
        }

        public void UpdateChart(DateTime now)
        {
            double headYL = MiscModal.me.GetValue(MiscType.HeadYL);
            double tailYL = MiscModal.me.GetValue(MiscType.TailYL);
            double deltaYL = headYL - tailYL;

            this.chart1.Series[0].LegendText = AppConfig.HeadName + ":" + headYL.ToString("F3");
            this.chart1.Series[1].LegendText = AppConfig.TailName + ":" + tailYL.ToString("F3");
            this.chart1.Legends[0].CustomItems[0].Name = "压差：" + deltaYL.ToString("F3");


            if (!DsHead.Ready)
            {
                DsHead.Initailize(this.chart1, 0);
            }
            else
            {
                DsHead.Add(now, headYL).Bind(this.chart1, 0);
            }

            
            if (!DsTail.Ready)
            {
                DsTail.Initailize(this.chart1, 1);
            }
            else
            {
                DsTail.Add(now, tailYL).Bind(this.chart1, 1);
            }

            // 留白处理

            try
            {
                if (DsHead.Ready && DsTail.Ready)
                {
                    var max = Math.Max(DsHead.MaxY, DsTail.MaxY);
                    if (double.IsNaN(max))
                        max = 100;

                    var min = Math.Min(DsHead.MinY, DsTail.MinY);
                    if (double.IsNaN(min))
                        min = -100;

                    double delta = Math.Abs(max - min) / 4;
                    if (delta > 1)
                    {
                        delta = (int)delta;
                    }

                    this.chart1.ChartAreas[0].AxisY.Maximum = max + delta;
                    this.chart1.ChartAreas[0].AxisY.Minimum = min - delta;
                }
            }
            catch
            {
                ;
            }
        }
    }
}
